package com.example.restatr.Atcivity.LogisticsQuery;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.example.restatr.Atcivity.Main.MainActivity;
import com.example.restatr.Atcivity.LogisticsQuery.Bean.LogisticsComplaintsBean;
import com.example.restatr.R;
import com.example.restatr.Tools;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.List;

public class MyComplaintsListAt extends AppCompatActivity {
    private Toolbar toolbar;
    private TextView barTitle;
    private EditText search;
    private ListView lv;
    private List<LogisticsComplaintsBean.RowsBean> rowsBeans = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_logistics_complaints_at);

        toolbar = findViewById(R.id.toolbar);
        barTitle = findViewById(R.id.bar_Title);
        search = findViewById(R.id.search);
        lv = findViewById(R.id.lv);

        Tools.SetBarTitle(toolbar, barTitle, "我的投诉", search, this, false);
    }

    @Override
    protected void onResume() {
        super.onResume();
        GetComplaints();
    }

    private void GetComplaints() {
        new Thread(() -> {
            String token = Tools.Get("/prod-api/api/logistics-inquiry/logistics_complaint/my-list", MainActivity.sp.getString("token", ""));
            LogisticsComplaintsBean logisticsComplaintsBean = new Gson().fromJson(token, LogisticsComplaintsBean.class);
            rowsBeans.addAll(logisticsComplaintsBean.getRows());
            runOnUiThread(() -> {
                if (rowsBeans.size() > 0) {
                    lv.setAdapter(new adapter());
                }
            });
        }).start();
    }

    class adapter extends BaseAdapter {

        @Override
        public int getCount() {
            return rowsBeans.size();
        }

        @Override
        public Object getItem(int position) {
            return rowsBeans.get(position);
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            convertView = getLayoutInflater().inflate(R.layout.zfz_list_item, null);
            ImageView tupian = convertView.findViewById(R.id.tupian);
            TextView name = convertView.findViewById(R.id.name);
            TextView diz = convertView.findViewById(R.id.diz);
            TextView phone = convertView.findViewById(R.id.phone);
            TextView mj = convertView.findViewById(R.id.mj);

            try {
                if (rowsBeans.get(position).getCompany().getImgUrl() != null) {
                    Glide.with(MyComplaintsListAt.this)
                            .load(Tools.ip + rowsBeans.get(position).getCompany().getImgUrl())
                            .transform(new RoundedCorners(15))
                            .into(tupian);
                } else {
                    tupian.setVisibility(View.GONE);
                }

                if (rowsBeans.get(position).getCompany().getName() != null) {
                    name.setText(rowsBeans.get(position).getCompany().getName());
                } else {
                    name.setText("未知快递");
                }
            } catch (Exception e) {
                e.printStackTrace();
            }

            diz.setText(rowsBeans.get(position).getInfoNo());
            phone.setText(rowsBeans.get(position).getDescription());
            String type = "";
            try {
                switch (rowsBeans.get(position).getQuestionType()) {
                    case "0":
                        type = "快递丢失";
                        break;
                    case "1":
                        type = "快递破损";
                        break;
                    case "2":
                        type = "快递被拆封";
                        break;
                    case "3":
                        type = "工作人员态度恶劣";
                        break;
                    case "4":
                        type = "未送货上门";
                        break;
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            mj.setText(type);

            return convertView;
        }
    }
}